<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Common extends MY_Controller {

	/**
	 * @var AdmSettingModel
	 */
	public $mSetting;
	public function __construct(){
		parent::__construct();
        $this->loadModel('AdmSettingModel', 'mSetting', 'admin');
	}

    /**
     * 区域联动
     */
    public function getAjaxAddress(){


        $id = post('address_id');
        $id = explode(',', $id);
        $arr = $this->mSetting->getLocationNameByIds($id);
        $string = '';
        foreach ($arr as $k => $v){
            $string .= ' '.$v['name'];
        }
        echo trim($string);
        exit;
    }

    /**
     * 店铺导航
     */
    public function merchantMap(){

        $lat = trim(get('lat'));
        $lng = trim(get('lng'));
        $address = '';
        $detAddress = '';
        $url = "http://restapi.amap.com/v3/geocode/regeo?output=json&location=".$lng.",".$lat."&key=45b5dd708554af695987b8bed3b3284c&radius=1000&extensions=base";
        $result = $this->_request($url, array());
        $result = json_decode($result,true);

        if($result['status'] == '1'){
            $address = $result['regeocode']['formatted_address'];
            $detAddress = $result['regeocode']['addressComponent']['province'].$result['regeocode']['addressComponent']['district'].$result['regeocode']['addressComponent']['township'];
        }
        $data = array(
            'lat' => $lat,
            'lng' => $lng,
            'user_name' => trim(get('shop_name')),
            'mobile' => trim(get('mobile')),
            'address' => $address,
            'detAddress' => $detAddress,
        );

        $this->view('common/merchantMap',$data);
    }

    /**
     * 关于我们
     */
    public function aboutUs(){
        $tA = $this->mSetting->getExplain('about_us');
        $tA = str_replace('{base_url}', base_url(), $tA);

        $this->view('common/info',['html'=>$tA,'title' => '关于我们'],0,0,0,false);
    }

    /**
     * 帮助中心
     */
    public function helpUs(){
        $tA = $this->mSetting->getExplain('help_us');
        $tA = str_replace('{base_url}', base_url(), $tA);

        $this->view('common/info',['html'=>$tA,'title' => '帮助中心'],0,0,0,false);
    }

    /**
     * 注册协议
     */
    public function agreement(){
        $tA = $this->mSetting->getExplain('agreement');
        $tA = str_replace('{base_url}', base_url(), $tA);
        $this->view('common/info',['html'=>$tA,'title' => '注册协议'],0,0,0,false);
    }

    /**
     * 用户引导广告图
     */
    public function ads(){
        $config = $this->mSetting->get('consumer_ads');
        $tA = array();
        if($config){
            $tA = json_decode($config, true);
            foreach ($tA as $k=>$v){
                $tA[$k] = base_url($v);
            }
        }

        $this->view('common/ads', array('data' => $tA, 'url' => base_url().'consumer/app/home/share','title' => '商户加盟'));
    }

    /**
     * 请求接口
     *
     * @param	string	$url	请求路径
     * @param 	array 	$param	请求参数
     */
    private function _request($url, $param){

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        $data = curl_exec($curl);
        if(curl_errno($curl)){
            p(curl_error($curl));
            exit(array('status' => 0, 'data' => NULL, 'message' => curl_error($curl)));
        }
        curl_close($curl);
        return $data;
    }
}